/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.chart.netbeans;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

import twaver.DataBoxSelectionEvent;
import twaver.DataBoxSelectionListener;
import twaver.Element;
import twaver.Node;
import twaver.TDataBox;
import twaver.TWaverConst;
import twaver.chart.BarChart;
import twaver.chart.PieChart;
import twaver.chart.RadarChart;
import demo.DemoPane;

public class NetbeansSurveyDemo extends DemoPane {

	private Font scaleMajorTextFont = new Font("Dialog", Font.PLAIN, 11);
	private Font axisTextFont = new Font("Dialog", Font.PLAIN, 12);
	
	private boolean radarChangeFlag = true;
	private boolean barChangeFlag = true;
	
	private String[] overallAxisTooltip = {
			"The two releases are basically the same.",
			"NetBeans 6.5 is better than NetBeans 6.1.",
			"NetBeans 6.5 is worse than NetBeans 6.1.<br> There is the following showstopper:",
			"I do not know." };
	private String[] supportAxisTooltip = {
			"There are some problems but <br> these can wait until the next release.",
			"The quality of this feature meets <br> my expectations and is ready for release.",
			"It is not stable yet. There is <br> the following showstopper in this area:",
			"I do not use this support." };

	public NetbeansSurveyDemo() {
		this.setBorder(BorderFactory.createTitledBorder("Results from NetBeans 6.5 Community Acceptance survey"));
		this.setLayout(new GridLayout(1, 2, 5, 5));
		this.add(createOverallPanel());
		this.add(createSupportPanel());
	}

	public JSplitPane createOverallPanel() {
		TDataBox overallBox = new TDataBox();
		Element quality = addElement("Quality", Color.ORANGE, 0.77, 0.15, 0.03, 0.05, overallBox);
		Element performance = addElement("Performance", Color.GREEN, 0.71, 0.23, 0, 0.06, overallBox);
	
		RadarChart chart = new RadarChart(overallBox) {
			protected String getAxisToopTip(int index, String item) {
				return "<html>" + item + ":<br>" + overallAxisTooltip[index] + "</html>";
			}
		};
		setRadarChartAttribute(chart, "Better", "Equal", "Worse", "Don't Know");
		chart.setTitle("Overall Quality & Performance Improvements");
		chart.setShapeFillGradientColor(new Color(245, 245, 245));
		chart.setRingMajorStroke(TWaverConst.STROKE_SOLID_1);
		chart.setRingStyle(TWaverConst.RADAE_RING_STYLE_ELLIPSE);
	
		JPanel detail = new JPanel(new GridLayout(1, 2, 5, 5));
		PieChart qualityChart = createPieChart("Overall Quality", 134, 26, 5, 8);
		detail.add(qualityChart);
		PieChart performanceChart = createPieChart("Performance Improvements", 122, 40, 0, 8);
		detail.add(performanceChart);
	
		JPanel panel = new JPanel(new BorderLayout());
		JPanel controlPane = new JPanel(new FlowLayout());
		controlPane.setBackground(Color.WHITE);
		controlPane.add(createCheckBox(quality, overallBox, detail, qualityChart));
		controlPane.add(createCheckBox(performance, overallBox, detail, performanceChart));
		panel.add(chart, BorderLayout.CENTER);
		panel.add(controlPane, BorderLayout.SOUTH);
	
		JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		splitPane.setDividerSize(5);
		splitPane.setDividerLocation(380);
		splitPane.setTopComponent(panel);
		splitPane.setBottomComponent(detail);
		return splitPane;
	}

	public JSplitPane createSupportPanel() {
		Color phpColor = Color.ORANGE;
		Color ajaxColor = Color.BLUE;
		Color saveColor = Color.GREEN;
		TDataBox supportBox = new TDataBox();
		Element php = addElement("PHP", phpColor, 0.18, 0.07, 0.01, 0.74, supportBox);
		Element ajax = addElement("Ajax & JavaScript", ajaxColor, 0.30, 0.16, 0, 0.54, supportBox);
		Element save = addElement("Save", saveColor, 0.40, 0.25, 0.01, 0.34, supportBox);

		RadarChart chart = new RadarChart(supportBox) {
			protected String getAxisToopTip(int index, String item) {
				return "<html>" + item + ":<br>" + supportAxisTooltip[index] + "</html>";
			}
		};
		chart.setTitle("<html>"+
				"PHP & "+
				"Ajax Support <br> "+
				"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; & Compile/Deploy on Save "+
				"</html>");
		setRadarChartAttribute(chart, "Good", "Acceptable", "Unstable", "Don't Use");
		chart.setAxisInflexionVisible(true);
		chart.setRingMajorStroke(TWaverConst.STROKE_SQUARE_THINNEST);

		TDataBox box = new TDataBox();
		Element phpElement = addElement("PHP", phpColor, 32, 12, 1, 128, box);
		Element ajaxElement = addElement("Ajax & JavaScript", ajaxColor, 52, 27, 0, 94, box);
		Element saveElement = addElement("Save", saveColor, 69, 43, 2, 59, box);

		BarChart barChart = new BarChart(box);
		barChart.setBarType(TWaverConst.BAR_TYPE_GROUP);
		barChart.addXScaleText(" Good");
		barChart.addXScaleText(" Acceptable");
		barChart.addXScaleText(" Unstable");
		barChart.addXScaleText(" Don't Use");
		barChart.setUpperLimit(140);
		barChart.setYScaleTextVisible(true);
		barChart.setYScaleValueGapAutoCalculate(true);

		this.addSelectionChangeListener(supportBox, box);

		JPanel panel = new JPanel(new BorderLayout());
		JPanel controlPane = new JPanel(new FlowLayout());
		controlPane.setBackground(Color.WHITE);
		controlPane.add(createSupportCheckBox(supportBox, box, php, phpElement));
		controlPane.add(createSupportCheckBox(supportBox, box, ajax, ajaxElement));
		controlPane.add(createSupportCheckBox(supportBox, box, save, saveElement));
		panel.add(chart, BorderLayout.CENTER);
		panel.add(controlPane, BorderLayout.SOUTH);

		JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		splitPane.setDividerSize(5);
		splitPane.setDividerLocation(380);
		splitPane.setTopComponent(panel);
		splitPane.setBottomComponent(barChart);
		return splitPane;
	}
	
	public JCheckBox createSupportCheckBox(
			final TDataBox topBox, final TDataBox bottomBox,
			final Element topElement, final Element bottomElement) {
		final JCheckBox checkBox = new JCheckBox(topElement.getName(), topBox.contains(topElement));
		checkBox.setBorder(null);
		checkBox.setBackground(Color.WHITE);
		checkBox.setForeground(topElement.getChartColor());
		checkBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (checkBox.isSelected()) {
					topBox.addElement(topElement);
					bottomBox.addElement(bottomElement);
				} else {
					topBox.removeElement(topElement);
					bottomBox.removeElement(bottomElement);
				}
			}
		});
		return checkBox;
	}

	public void addSelectionChangeListener(final TDataBox topBox, final TDataBox bottomBox) {
		topBox.getSelectionModel().addDataBoxSelectionListener(new DataBoxSelectionListener(){
			public void selectionChanged(DataBoxSelectionEvent e) {
				if(radarChangeFlag){
					radarChangeFlag = false;
					List list = topBox.getSelectionModel().getAllSelectedElement();
					bottomBox.getSelectionModel().clearSelection();
					if (list != null) {
						int size = list.size();
						for (int i = 0; i < size; i++) {
							Element element = (Element) list.get(i);
							bottomBox.getSelectionModel().appendSelection(bottomBox.getElementByID(element.getID()));
						}
					}
					radarChangeFlag = true;
				}
			}
		});
		
		bottomBox.getSelectionModel().addDataBoxSelectionListener(new DataBoxSelectionListener(){
			public void selectionChanged(DataBoxSelectionEvent e) {
				if(barChangeFlag){
					barChangeFlag = false;
					List list = bottomBox.getSelectionModel().getAllSelectedElement();
					topBox.getSelectionModel().clearSelection();
					if (list != null) {
						int size = list.size();
						for (int i = 0; i < size; i++) {
							Element element = (Element) list.get(i);
							topBox.getSelectionModel().appendSelection(topBox.getElementByID(element.getID()));
						}
					}
					barChangeFlag = true;
				}
			}
		});
	}
	
	public void setRadarChartAttribute(RadarChart chart, String axisText1, String axisText2, String axisText3, String axisText4) {
		chart.setAxisStroke(TWaverConst.STROKE_SOLID_1);
		chart.getLegendPane().setVisible(false);
		chart.setRadarFill(true);
		chart.setRadarFillGradient(false);
		chart.getTitleLabel().setForeground(Color.BLUE.brighter());
		chart.setScaleMajorTextColor(new Color(70, 190, 240));
		chart.setScaleMajorTextFont(scaleMajorTextFont);
		chart.setAxisTextFont(axisTextFont);
		chart.setRadarFillColor(new Color(245, 245, 245));
		chart.setScaleMaxValue(0.8);
		chart.setScaleMajorCount(4);
		chart.setAxisStartAngle(-90);
		chart.setRingMinVisible(false);
		chart.addAxisText(axisText1);
		chart.addAxisText(axisText2);
		chart.addAxisText(axisText3);
		chart.addAxisText(axisText4);
		chart.setAxisTextTooltipVisible(true);
	}

	public Element addElement(String name, Color color, double value1, double value2, double value3, double value4, TDataBox box) {
		Element element = new Node(name);
		element.setName(name);
		element.putChartColor(color);
		element.addChartValue(value1);
		element.addChartValue(value2);
		element.addChartValue(value3);
		element.addChartValue(value4);
		box.addElement(element);
		return element;
	}

	public PieChart createPieChart(String title, double value1, double value2, double value3, double value4) {
		TDataBox box = new TDataBox();
		PieChart chart = new PieChart(box);
		chart.getTitleLabel().setForeground(new Color(0, 0, 160));
		chart.setTitle(title);
		chart.setValueTextPercent(true);
		addPieElement(box, " Better", value1, Color.GREEN);
		addPieElement(box, " Equal", value2, Color.YELLOW);
		addPieElement(box, " Worse", value3, Color.RED);
		addPieElement(box, " Don't Know", value4, Color.PINK);
		return chart;
	}

	private void addPieElement(TDataBox box, String name, double value, Color color) {
		Element element = new Node();
		element.setName(name);
		element.putChartValue(value);
		element.putChartColor(color);
		box.addElement(element);
	}

	private JCheckBox createCheckBox(final Element element, final TDataBox box, final JPanel container, final JPanel chartPanel) {
		final JCheckBox checkBox = new JCheckBox(element.getName(), box.contains(element));
		checkBox.setBorder(null);
		checkBox.setBackground(Color.WHITE);
		checkBox.setForeground(element.getChartColor());
		checkBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (checkBox.isSelected()) {
					box.addElement(element);
					container.add(chartPanel);
				} else {
					box.removeElement(element);
					container.remove(chartPanel);
				}
				container.validate();
				container.repaint();
			}
		});
		return checkBox;
	}
}